v1.0.0: Initial implementation#2
Open
mkenney wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the initial model implementation by expanding core Model functionality (sorting, mapping/filtering/reducing, merging, JSON marshal/unmarshal, iterators) and modernizing types/comments (e.g., any and Go doc comments).
Changes:
- Added/expanded model operations: sort (by key/value + reverse), merge, map/filter/reduce, and iterator improvements.
- Refactored internal typing from
interface{}toanyand improved error propagation in import/unmarshal paths. - Updated tests/examples and module dependency on
github.com/bdlm/cast/v2.
Reviewed changes
Copilot reviewed 12 out of 13 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| value.go | Switches internal storage/API returns to any and converts block comments to Go doc comments. |
| model.types.go | Introduces a generic-ish ModelData type constraint. |
| model.sorter.go | Reworks sorting implementation; adds Len, Reverse, and value-comparison helpers. |
| model.marshaler.go | Adds locking during marshal and improves unmarshal/import error handling; updates UnmarshalModel signature/behavior. |
| model.iterator.go | Updates iterator APIs to any and improves list Seek validation. |
| model.importer.go | Refactors import helpers to return errors and uses any-typed containers. |
| model.go | Expands core Model API (New accepts initial data, GetData copies out state, adds Map/Filter/Reduce/Merge, adds read-only enforcement, etc.). |
| model_test.go | Updates construction/signatures and fixes an assertion. |
| interface.marshaler.go | Converts block comments to doc comments; updates UnmarshalModel docs/signature. |
| go.mod | Bumps github.com/bdlm/cast/v2 dependency version. |
| examples_test.go | Updates usage to new New(..., nil) and GetData(). |
| errors.go | Adds additional error definitions and converts header comment to doc comment style. |
| .gitignore | Expands ignore patterns (but includes duplicated entries). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+7
to
+9
| type ModelData interface { | ||
| cast.Tslice | cast.Tmap | ||
| } |
| } | ||
|
|
||
| // Hash model | ||
| hashKey := pos.(string) |
Comment on lines
58
to
64
| if stdModel.ModelTypeList == mdl.GetType() { | ||
| k := key.(int) | ||
| if k > len(mdl.data) { | ||
| k := cast.To[int](key) | ||
| if k < 0 || k >= len(mdl.data) { | ||
| return errors.WrapE(InvalidIndex, errors.Errorf("index '%d' out of range", k)) | ||
| } | ||
| mdl.data = append(mdl.data[:key.(int)-1], mdl.data[key.(int):]...) | ||
| mdl.data = append(mdl.data[:k], mdl.data[k+1:]...) | ||
| return nil |
Comment on lines
354
to
379
| @@ -285,14 +379,32 @@ func (mdl *Model) SetData(data any) error { | |||
| return nil | |||
| Sort sorts the model data. | ||
| */ | ||
| // Len returns the number of items stored in this model. | ||
| func (mdl *Model) Len() int { |
Comment on lines
+44
to
+58
| // Sort sorts the model data by the specified flag. | ||
| func (mdl *Model) Sort(flag stdSorter.SortFlag) error { | ||
| data := []interface{}{} | ||
| hashIdx := map[string]int{} | ||
| idxHash := map[int]string{} | ||
| switch flag { | ||
| case stdSorter.SortByKey: | ||
| if mdl.locked { | ||
| return errors.WrapE(ReadOnlyProperty, errors.Errorf("model is locked")) | ||
| } | ||
|
|
||
| if flag&stdSorter.SortByKey != 0 { | ||
| if stdModel.ModelTypeHash == mdl.GetType() { | ||
| order := []string{} | ||
| for _, v := range mdl.idxHash { | ||
| order = append(order, v) | ||
| keys := make([]string, 0, len(mdl.idxHash)) | ||
| for _, k := range mdl.idxHash { | ||
| keys = append(keys, k) | ||
| } | ||
| sort.Strings(order) | ||
| for _, v := range order { | ||
| hashIdx[v] = len(data) | ||
| idxHash[len(data)] = v | ||
| data = append(data, mdl.data[mdl.hashIdx[v]]) | ||
| sort.Strings(keys) | ||
| mdl.data, mdl.hashIdx, mdl.idxHash = rebuildHashIndexes(keys, mdl) | ||
| } |
Comment on lines
14
to
21
| if stdModel.ModelTypeList == mdl.GetType() { | ||
| return json.Marshal(mdl.data) | ||
| } | ||
| d := map[string]interface{}{} | ||
| d := map[string]any{} | ||
| for k, v := range mdl.data { | ||
| d[mdl.idxHash[k]] = v | ||
| } | ||
| return json.Marshal(d) |
Comment on lines
124
to
132
| case int, int8, int16, int32, int64: | ||
| mdl.mux.Lock() | ||
| defer mdl.mux.Unlock() | ||
| if key.(int) >= int(len(mdl.data)) { | ||
| return nil, errors.WrapE(InvalidIndex, errors.Errorf("invalid index '%d'", key.(int))) | ||
| k := cast.To[int](key) | ||
| if k < 0 || k >= len(mdl.data) { | ||
| return nil, errors.WrapE(InvalidIndex, errors.Errorf("invalid index '%d'", k)) | ||
| } | ||
| ret := mdl.data[key.(int)] | ||
| return &Value{ret}, nil | ||
| return &Value{mdl.data[k]}, nil | ||
| default: |
Comment on lines
368
to
379
| @@ -285,14 +379,32 @@ func (mdl *Model) SetData(data any) error { | |||
| return nil | |||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.